//
//  MCBreadcrumbTrailView.m
//  XDYL
//
//  Created by menhao on 2018/3/21.
//  Copyright © 2018年 TLYL01. All rights reserved.
//

#import "MCBreadcrumbTrailView.h"
#import "MCHongBaoPersonCollectionViewCell.h"

@interface MCBreadcrumbTrailView()<UICollectionViewDelegate,UICollectionViewDataSource>


@end

@implementation MCBreadcrumbTrailView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self setUpUI];
    }
    return self;
}

- (void)setUpUI{
    UILabel *label = [[UILabel alloc] init];
    [self addSubview:label];
    label.text = @"会员层级:";
    label.textColor =RGB(68, 68,68);
    label.backgroundColor = RGB(255, 255, 255);
    label.font = [UIFont systemFontOfSize:12];
    label.frame = CGRectMake(13, 0, 55, MC_REALVALUE(30));
    label.textAlignment = NSTextAlignmentCenter;
    UICollectionViewFlowLayout  *layout = [[UICollectionViewFlowLayout alloc] init];
    // 设置具体属性
    // 1.设置 最小行间距
    layout.minimumLineSpacing = 0;
    //    // 2.设置 最小列间距
    layout. minimumInteritemSpacing  = 0;
    //    // 3.设置item块的大小 (可以用于自适应)
    //    layout.estimatedItemSize = CGSizeMake(20, self.heiht);
    //    layout.estimatedItemSize = CGSizeMake(50,50);
    // 设置滑动的方向 (默认是竖着滑动的)
    layout.scrollDirection =  UICollectionViewScrollDirectionHorizontal;
    // 设置item的内边距
    //    layout.sectionInset = UIEdgeInsetsMake(10,10,10,10);
    self.collection = [[UICollectionView alloc] initWithFrame:CGRectMake(68, 0, self.widht - MC_REALVALUE(26) - 55,MC_REALVALUE(30)) collectionViewLayout:layout];
    //    self.heiht
    self.collection.delegate = self;
    self.collection.dataSource = self;
    //    [self.collection setPrefetchingEnabled:YES];
    //    self.collection.
    [self addSubview:self.collection];
    [self.collection registerClass:[MCHongBaoPersonCollectionViewCell class] forCellWithReuseIdentifier:@"SelfSizingCollectCell"];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshUI) name:@"selectedBreadcrumbTrailBlock" object:nil];
    UILabel *line = [[UILabel alloc] init];
    [self addSubview:line];
    
    line.backgroundColor =RGB(220, 220,220);
    
    line.frame = CGRectMake(0, MC_REALVALUE(29), G_SCREENWIDTH, MC_REALVALUE(1));
    self.collection.backgroundColor =  RGB(249, 249, 249);
    //    if
    //    if #available(iOS 10.0, *) {
    //        collectionView?.isPrefetchingEnabled = false
    //    }
}
- (void)refreshUI{
    [self.collection reloadData];
}
#pragma MARK --- UICollectionViewDelegate
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return [self.dataArr count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    MCHongBaoPersonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SelfSizingCollectCell" forIndexPath:indexPath];
    cell.textLabel.text = self.dataArr[indexPath.row];
    cell.textLabel.textColor = RGB(54, 128, 211);
    cell.imageV.hidden = NO;
    if (indexPath.row == self.dataArr.count - 1) {
        cell.imageV.hidden = YES;
    } else {
        cell.imageV.hidden = NO;
    }
    cell.backgroundColor =  RGB(249, 249, 249);
    return cell;
}

- (CGFloat)calculateRowWidth:(NSString *)string {
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:MC_REALVALUE(12)]};  //指定字号
    CGRect rect = [string boundingRectWithSize:CGSizeMake(MAXFLOAT, MC_REALVALUE(30))/*计算宽度时要确定高度*/ options:NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading attributes:dic context:nil];
    return rect.size.width;
}

//设置item的size
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    NSString *str = self.dataArr[indexPath.row];
    CGFloat W = ceilf([self calculateRowWidth:str]) + MC_REALVALUE(20);
    CGFloat H = MC_REALVALUE(30);
    return CGSizeMake(W, H);
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.selectedBreadcrumbTrailBlock) {
        self.selectedBreadcrumbTrailBlock(indexPath.row);
    }
}



@end
